package com.bolingcavalry.crm.sysdict.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.base.PageResult;
import com.base.ResponseResult;
import com.bolingcavalry.crm.sysdict.service.SysDictItemServiceImpl;
import com.dto.fault.SysDictItemDTO;
import com.vo.fault.SysDictItemVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "字典管理")
@RestController
@RequestMapping("/fault")
@Slf4j
public class SysDictItemController {
    
    @Autowired
    private SysDictItemServiceImpl sysDictItemServiceImpl;
    
    /**
     * 新增故障现象
     */
    @PostMapping("/add")
    @ApiOperation("新增故障现象")
    public ResponseResult add(@RequestBody SysDictItemDTO faultPhenomenonDTO) {
        log.info("新增故障现象: {}", faultPhenomenonDTO);
        return sysDictItemServiceImpl.add(faultPhenomenonDTO);
    }
    
    /**
     * 删除故障现象
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除故障现象")
    public ResponseResult delete(@PathVariable Long id) {
        log.info("删除故障现象: {}", id);
        return sysDictItemServiceImpl.deleteById(id);
    }
    
    /**
     * 批量删除故障现象
     */
    @DeleteMapping("/batch-delete")
    @ApiOperation("批量删除故障现象")
    public ResponseResult batchDelete(@RequestParam List<Long> ids) {
        log.info("批量删除故障现象: {}", ids);
        return sysDictItemServiceImpl.batchDelete(ids);
    }
    
    /**
     * 更新故障现象
     */
    @PostMapping("/update")
    @ApiOperation("更新故障现象")
    public ResponseResult update(@RequestBody SysDictItemDTO faultPhenomenonDTO) {
        log.info("更新故障现象: {}", faultPhenomenonDTO);
        return sysDictItemServiceImpl.update(faultPhenomenonDTO);
    }
    
    /**
     * 根据ID获取故障现象详情
     */
    @GetMapping("/get/{id}")
    @ApiOperation("根据ID获取故障现象详情")
    public ResponseResult<SysDictItemVO> get(@PathVariable Long id) {
        log.info("获取故障现象详情: {}", id);
        return ResponseResult.success(sysDictItemServiceImpl.getById(id));
    }
    
    /**
     * 分页条件查询
     */
    @GetMapping("/list")
    @ApiOperation("分页条件查询故障现象")
    public ResponseResult<PageResult<SysDictItemVO>> list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String faultDescription) {
        
        IPage<SysDictItemVO> page = sysDictItemServiceImpl.getByConditions(pageNum, pageSize, faultDescription);
        
        PageResult<SysDictItemVO> result = new PageResult<>();
        result.setTotal(page.getTotal());
        result.setPages((int) page.getPages());
        result.setCurrent((int) page.getCurrent());
        result.setSize((int) page.getSize());
        result.setRecords(page.getRecords());
        
        return ResponseResult.success("查询成功", result);
    }
    
    /**
     * 查询所有故障现象列表
     */
    @GetMapping("/list-all")
    @ApiOperation("查询所有故障现象列表")
    public ResponseResult<List<SysDictItemVO>> listAll() {
        List<SysDictItemVO> list = sysDictItemServiceImpl.getAllList();
        return ResponseResult.success("查询成功", list);
    }

    /**
     * 查询全部故障现象列表，只返回故障现象
     */
    @GetMapping("/list-description")
    @ApiOperation("查询全部故障现象列表，只返回故障现象")
    public ResponseResult<List<String>> listAllFaultDescription() {
        List<String> list = sysDictItemServiceImpl.getAllFaultDescription();
        return ResponseResult.success("查询成功", list);
    }

    /**
     * 查询全部问题归类
     */
    @GetMapping("/list-category")
    @ApiOperation("查询全部问题归类")
    public ResponseResult<List<String>> listAllCategory() {
        List<String> list = sysDictItemServiceImpl.getAllCategory();
        return ResponseResult.success("查询成功", list);
    }
}
